package com.cuit.xsc.weishare.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Spinner;

import com.cuit.xsc.weishare.R;
import com.cuit.xsc.weishare.adapter.UserSelecteAdapter;
import com.cuit.xsc.weishare.logic.MainService;
import com.cuit.xsc.weishare.model.UserInfo;
import com.cuit.xsc.weishare.services.UserInfoServices;
import com.cuit.xsc.weishare.util.ConfigUtil;
import com.cuit.xsc.weishare.util.LogUtil;

public class UserSelecteAct extends BaseActivity implements OnItemSelectedListener,OnClickListener
{
	private Spinner spinner = null;
	private UserSelecteAdapter adapter = null;
	private List<UserInfo> list = null;
	private UserInfoServices uServices = null;
	private ImageButton addUserBut = null;
	private ImageButton minusUserBut = null;
	private Button loginBut = null;
	private boolean isFirst = true;
	private String currentType = null;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_selecte);
		spinner = (Spinner) findViewById(R.id.spinner);
		addUserBut = (ImageButton) findViewById(R.id.add_user_but);
		minusUserBut = (ImageButton) findViewById(R.id.minus_user_but);
		loginBut = (Button) findViewById(R.id.login_but);
		
		currentType = getIntent().getStringExtra(ConfigUtil.TYPE);
		uServices = new UserInfoServices(this);
		list = findUserInfos(currentType);
		if (list != null)
		{
			
			adapter = new UserSelecteAdapter(this);
			adapter.setList(list);
			spinner.setAdapter(adapter);	
		}
		//spinner.setOnItemSelectedListener(this);
		addUserBut.setOnClickListener(this);
		minusUserBut.setOnClickListener(this);
		loginBut.setOnClickListener(this);
		
		
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		list = findUserInfos(currentType);
		adapter.setList(list);
		adapter.notifyDataSetChanged();
		
	}
	
	private List<UserInfo> findUserInfos(String userType)
	{
		return uServices.findUsersByType(userType);
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
	{	
		if (isFirst)
		{
			isFirst = false;
			if (list != null && list.size() >1)
			{
				return;
			}
			
		}
		MainService.setCurrentUser(list.get(position));
		Intent intent = new Intent(this, MainAct.class);
		LogUtil.i(this, "spinner choise "+position);
		startActivity(intent);
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent)
	{
		
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.login_but:
			UserInfo userInfo = (UserInfo) spinner.getSelectedItem();
			if (userInfo != null)
			{
				MainService.setCurrentUser(userInfo);
				Intent intent = new Intent(this, MainAct.class);
				startActivity(intent);
			}
			break;
		case R.id.add_user_but:
			Intent intent = new Intent(this, AuthAct.class);
			intent.putExtra(ConfigUtil.TYPE, currentType);
			startActivity(intent);
			finish();
			break;
		case R.id.minus_user_but:
			Intent mIntent = new Intent(this, AccountManagerAct.class);
			mIntent.putExtra(ConfigUtil.TYPE, currentType);
			startActivity(mIntent);
			break;

		default:
			break;
		}
		
	}

}
